﻿Imports ISNet.WebUI.WebGrid
Imports System.Data
Imports log4net

Partial Class mtFctr_View
    Inherits System.Web.UI.Page

    Public AlertText, ScriptText As String
    Private Shared logger As ILog = LogManager.GetLogger("mtFctr_View")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        'Session("PlanYear") = "2557"
        PlanYearsTxt.Text = Session("PlanYear")
        Session("PlanYear") = PlanYearsTxt.Text

        Label1.Text = "ระบบจัดทำงบประมาณ >> ตั้งค่าระบบ >> Fctr_View"
        'If Page.IsPostBack = False Then
        '    ScriptText = "SetFilter();"
        'Else
        '    ScriptText = ""
        'End If
    End Sub

    Protected Sub UserFC_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)
        'If Session("users") Is Nothing Then
        '    Response.Redirect("LoginForm.aspx")
        'End If

        Dim adap As New MasterDataSetTableAdapters.Users2TableAdapter
        Dim dt As New MasterDataSet.Users2DataTable
        dt = adap.GetDataBy1(Val(PlanYearsTxt.Text))
        'dt = adap.GetLevel1(Val(PlanYearsTxt.Text))
        e.DataSource = dt
    End Sub

    Protected Sub WebGrid1_InitializeDataSource(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.DataSourceEventArgs) Handles WebGrid1.InitializeDataSource
        If IsPostBack = True Then
            'Try
            '    Dim Fctr_ViewDataSet As New DataSet
            '    GetData(fctrWebCombo.Value, PlanYearsTxt.Text, Session("UserFC"), Fctr_ViewDataSet)
            '    e.DataSource = Fctr_ViewDataSet.Tables(0)
            'Catch ex As Exception
            '    logger.Error(ex.Message)
            '    logger.Error(ex.StackTrace)
            'End Try
        End If
        ' MsgBox(WebCombo1.Value)
    End Sub

    Protected Sub WebGrid1_InitializePostBack(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.PostbackEventArgs)
        If e.Action = PostBackAction.Custom Then
            'Dim id As String = WebGrid1.SelectedRowKeyValue
            'Dim PlanYear As Integer = PlanYearsTxt.Text
            'Dim CmmGroup1 As String = Session("CmmtGroup1")
            'DeleteSub(id)
            'WebGrid1.ClientAction.Refresh()

            'logger.Info("User " & Session("users") & " delete " & id.ToString)
        End If

    End Sub

    Sub Plot_Grid()
        Dim CMTDataSet As New DataSet
        GetData(fctrWebCombo.Value, PlanYearsTxt.Text, UserFC.Value, CMTDataSet)
        WebGrid1.DataSource = CMTDataSet.Tables(0)
        WebGrid1.DataBind()
        ''Session("CmmtGroup1") = GroupTextBox.Text
    End Sub

    Protected Sub SearhBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SearhBtn.Click
        Session("SearchTxt") = ""
        If fctrWebCombo.Value <> "" And fctrWebCombo.Text <> "" Then
            Session("SearchTxt") += " And FundCenterCode = '" & fctrWebCombo.Value & "'"
        Else
            Alert("กรุณาเลือก Fund Center")
            Exit Sub
        End If
       
        If UserFC.Value <> "" And UserFC.Text <> "" Then
            Session("SearchTxt") += " And UserName = '" & UserFC.Value & "'"
            'Response.Write(UserFC.Value)
        Else
            Alert("กรุณาเลือก User Name")
            Exit Sub
        End If

        Plot_Grid()
    End Sub

    Sub GetData(root_id As String, PlanYear As Decimal, UserFC As String, ByRef ds As DataSet)
        Dim dt As DataTable = ds.Tables.Add("Items")
        dt.Columns.Add("id", Type.GetType("System.Decimal"))
        dt.Columns.Add("FundCenterCode", Type.GetType("System.String"))
        dt.Columns.Add("Description", Type.GetType("System.String"))
        dt.Columns.Add("ParentCode", Type.GetType("System.Decimal"))
        dt.Columns.Add("level", Type.GetType("System.Decimal"))
        Dim adapter As New MasterDataSetTableAdapters.fctr_viewTableAdapter
        Dim datatable As New MasterDataSet.fctr_viewDataTable

        Dim cadapter As New MasterDataSetTableAdapters.FundCenters1TableAdapter
        Dim uadapter As New MasterDataSetTableAdapters.Users2TableAdapter
        ''Dim Id As Decimal
        ''If root_id = 0 ThenS
        ''    Exit Sub
        ''End If
        Dim id_main As Decimal
        ''datatable = adapter.GetDataByFCT_PY_User(root_id, PlanYear, Val(UserFC))
        ' Response.Write(datatable.Count)

        ''*********************************************

        Dim sql As String = "select * from fctr_view "
        sql += " WHERE 1 = 1 "
        sql += " And PlanYear = " & Val(PlanYearsTxt.Text)
        'sql += " And FundCenterCode = '" & root_id & "'"
        'sql += " And UserFundCenters_Id = " & UserFC
        sql += Session("SearchTxt")
        sql += " ORDER BY PlanYear"

        adapter.Searchfctr_view(datatable, sql)
        'Response.Write(sql)

        ''*********************************************

        For Each row As MasterDataSet.fctr_viewRow In datatable
            ' Id = row.fcrt_view_id
            Dim r As DataRow
            r = dt.NewRow()
            r("FundCenterCode") = If(row.IsFundCenterCodeNull, "", row.FundCenterCode)
            r("Description") = If(row.IsFundCenterCodeNull, "", row.FundCenterCode) & "  " & cadapter.GetName(row.FundCenterCode, PlanYear) '& " / " & If(row.IsUserNameNull, "", uadapter.GetUsersName(row.UserName, PlanYear))
            'Response.Write(row.Cmmt_CmmtCode & " " & PlanYear)
            r("ParentCode") = 0
            r("id") = row.fcrt_view_id
            id_main = row.fcrt_view_id
            ''r("level") = row.Level
            dt.Rows.Add(r)
        Next

        datatable = adapter.GetDataByParentID(root_id, Val(PlanYear), UserFC)

        For Each row As MasterDataSet.fctr_viewRow In datatable
            GetChild(row.fcrt_view_id, ds, dt, PlanYear, UserFC, id_main)
        Next
    End Sub

    Sub GetChild(ByVal cid As Decimal, ByRef ds As DataSet, ByRef dt As DataTable, PlanYear As Decimal, UserFC As String, parent_ID As Decimal)
        Dim Madapter As New MasterDataSet2TableAdapters.CmmtsTableAdapter
        Dim adapter As New MasterDataSetTableAdapters.fctr_viewTableAdapter
        Dim datatable As New MasterDataSet.fctr_viewDataTable

        Dim cadapter As New MasterDataSetTableAdapters.FundCenters1TableAdapter
        Dim uadapter As New MasterDataSetTableAdapters.Users2TableAdapter

        datatable = adapter.GetDataByID(cid)
        Dim r As DataRow
        Dim version As Integer = 1
        r = dt.NewRow()

        Dim Id As Decimal
        Dim fct As String = ""
        For Each row As MasterDataSet.fctr_viewRow In datatable
            fct = row.FundCenterCode
            Try
                r("FundCenterCode") = If(row.IsFundCenterCodeNull, "", row.FundCenterCode)
                r("Description") = If(row.IsFundCenterCodeNull, "", row.FundCenterCode) & "  " & cadapter.GetName(row.FundCenterCode, PlanYear) '& " / " & If(row.IsUserNameNull, "", uadapter.GetUsersName(row.UserName, PlanYear))
                r("ParentCode") = parent_ID
            Catch ex As Exception
                logger.Error(ex.Message)
                logger.Error(ex.StackTrace)
            End Try
            ''r("level") = row.Level
            r("id") = row.fcrt_view_id
            Id = row.fcrt_view_id
        Next
        dt.Rows.Add(r)
        ''datatable = adapter.GetDataByParentID(Id, PlanYear, CmmGroup)
        datatable = adapter.GetDataByParentID(fct, PlanYear, UserFC)
        For Each row As MasterDataSet.fctr_viewRow In datatable

            GetChild(row.fcrt_view_id, ds, dt, PlanYear, UserFC, Id)
        Next
    End Sub

    Protected Sub WebGrid1_InitializeRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        If e.Row.Type = RowType.Record Then
            'Dim dr As DataRowView = DirectCast(e.Row.DataRow, DataRowView)
            'Dim level As Decimal = dr("level")
            'If level = 3 Then
            '    Dim ImgButton As ImageButton
            '    ImgButton = CType(e.Row.Cells.GetNamedItem("Action").GetTemplateContainer().FindControl("ImageButton1"), Image)
            '    ImgButton.Visible = False
            'End If
            'e.Row.ExpandSelfRefRow()
        End If
    End Sub

    Sub Alert(ByVal message As String)
        AlertText += "<script> alert('" & message & "');</script>"
    End Sub
End Class
